﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.CourseD
{
    public class CourseDAL
    {
        private readonly StarsEASContext _context;

        public CourseDAL(StarsEASContext context) { 
            _context = context;
        }

        public ResultUtils GetAllCourse() {
            StarsEASContext _context = new StarsEASContext();
            List<Course> courseList =  _context.Courses.ToList();
            if (courseList == null)
            {
                AppLog.WriteError($"查询课程信息---未查询到任何课程信息", true);
                return new ResultUtils() { code = 500, message = "null" };
            }
            AppLog.WriteInfo($"查询课程信息---查询到{courseList.Count()}条课程信息", true);
            return new ResultUtils() { code=200,message=courseList};
        }

        public ResultUtils InsertCourse(List<Course> data) {
            StarsEASContext _context = new StarsEASContext();
            if (data == null) {
                AppLog.WriteError($"新增课程信息---课程列表为空", true);
                return new ResultUtils(){ code = 500, message = "课程列表信息错误" };
            }
            foreach (Course course in data)
            {
                Course temp = _context.Courses.Where<Course>(c => c.CourseName == course.CourseName).FirstOrDefault();
                if (temp == null)
                {
                    course.CourseId = 0;
                    _context.Courses.Add(course);
                    AppLog.WriteInfo($"新增课程信息---课程{course.CourseName}开始新增", true);
                }
                else {
                    AppLog.WriteError($"新增课程信息---课程{temp.CourseName}已存在", true);
                }
            }
            int result = _context.SaveChanges();
            AppLog.WriteInfo($"新增课程信息---成功新增{result}条课程信息", true);
            return new ResultUtils() { code = 200, message = result };
        }

        public ResultUtils UpdataCourse(Course data) {
            StarsEASContext _context = new StarsEASContext();
            Course course = _context.Courses.Where<Course>(c => c.CourseId==data.CourseId).First();
            if (course == null)
            {
                AppLog.WriteError($"修改课程信息---{data.CourseId}未查询到课程信息", true);
                return new ResultUtils() { code = 500, message = "Error" };
            }
            course.CourseCredits = data.CourseCredits??course.CourseCredits;
            course.CourseName = data.CourseName??course.CourseName;
            course.CourseAttribute = data.CourseAttribute??course.CourseAttribute;
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"修改课程信息---{data.CourseId}未查询到课程信息", true);
                return new ResultUtils() { code=500, message = "修改失败或与原数据一致" };
            }
            AppLog.WriteInfo($"修改课程信息---{data.CourseId}修改成功", true);
            return new ResultUtils(){ code = 200, message = "修改成功"};
        }





    }
}
